﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace Sogeti.QueryWrapper
{

    public class WI
    {
        public string Title;
        public string Description;
        public string Type;
        public string AreaPath;
        public string IterationPath;
        public Dictionary<string, object>   customFields;


        public WI()
        {
            customFields = new Dictionary<string, object>();
        }

    }

    public class TPWiWrapper
    {

        protected TfsTeamProjectCollection tpCollection;
        protected string teamProjectName;
        protected WorkItemStore         wiStore;
        protected QueryHierarchy        qh;
        protected WorkItemTypeCollection wiTypes;

        public TPWiWrapper(string tpCollectionUrl, string aTeamProjectName)
        {

            tpCollection = new TfsTeamProjectCollection(new Uri(tpCollectionUrl));
            teamProjectName = aTeamProjectName;
            
            wiStore = (WorkItemStore)tpCollection.GetService(typeof(WorkItemStore));
            qh = wiStore.Projects[teamProjectName].QueryHierarchy;
            wiTypes= wiStore.Projects[teamProjectName].WorkItemTypes;

        }



        public List<WI> GetInitialWorkItens()
        {
            string s = "SELECT [System.TeamProject], [System.Id],  [System.AssignedTo], [System.Title], [System.CreatedBy], [System.CreatedDate] FROM WorkItems WHERE [System.TeamProject]='" + teamProjectName + "'  ORDER BY [System.Id]";

            WorkItemCollection existingWI = wiStore.Query(s);
            DateTime dtCreated = existingWI[0].CreatedDate;
            String sCreatedBy = existingWI[0].CreatedBy;


            List<WI> lst = new List<WI>();

                        
            foreach (WorkItem itm in existingWI)
            {
                if (itm.CreatedBy == sCreatedBy && itm.CreatedDate == dtCreated)
                {
                    WI wi= new WI { Title = itm.Title, Description = itm.Description, Type = itm.Type.Name, AreaPath = itm.AreaPath, IterationPath = itm.IterationPath };
                    foreach(Field fld in itm.Fields)
                    {
                        if (fld.ReferenceName.IndexOf("System.") < 0 && fld.ReferenceName.IndexOf("Microsoft.")<0)
                        {
                            wi.customFields.Add(fld.ReferenceName, fld.Value);

                        }

                    }
                    
                    lst.Add(wi);

                }
                else
                {
                    break;
                }

            }

            return lst;
        }


        public void CreatWorkItems(List<WI> lst)
        {

            foreach (WI itm in lst)
            {
                    WorkItem newItm =  new WorkItem(wiTypes[itm.Type]);
                    newItm.Title= itm.Title;
                    newItm.Description= itm.Description;
                    newItm.AreaPath= itm.AreaPath;
                    newItm.IterationPath= itm.IterationPath;
                    foreach (KeyValuePair<string, object> kvp in itm.customFields)
                    {
                        newItm.Fields[kvp.Key].Value = kvp.Value;
                    }
                    
                    foreach (Field fld in newItm.Fields)
                    {
                        if (!fld.IsValid)
                        {
                            Console.WriteLine(fld.Name + " " + fld.Value);
                        }
                    }
                    newItm.Save();
                }

            }

            
    }
    
}
